package cn.yh.nft.turbo.collection.domain.entity;

import cn.yh.nft.turbo.api.collection.constant.CollectionStateEnum;
import cn.yh.nft.turbo.api.collection.request.CollectionCreateRequest;
import cn.yh.nft.turbo.collection.domain.entity.convertor.CollectionConvertor;
import cn.yh.nft.turbo.datasource.domain.entity.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import org.dromara.easyes.annotation.IndexName;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * <p>
 *     藏品信息
 * </p>
 * @ClassName: Collection
 * @Author: 草莓爱上啵啵
 */
@Getter
@Setter
@Document(indexName = "nfturbo_collection")
@IndexName(value = "nfturbo_collection")
public class Collection extends BaseEntity {

    /**
     * 藏品名称
     */
    private String name;

    /**
     * 藏品封面
     */
    private String cover;

    /**
     * 藏品类目id
     */
    private String classId;

    /**
     * 价格
     */
    private BigDecimal price;

    /**
     * 藏品数量
     */
    private Integer quantity;

    /**
     * 藏品详情
     */
    private String detail;

    /**
     * 可售库存
     */
    @Field(name = "saleable_inventory", type = FieldType.Long)
    private Long saleableInventory;

    /**
     * 已占库存
     */
    @Deprecated
    private Long occupiedInventory;

    /**
     * 状态
     */
    private CollectionStateEnum state;

    /**
     * 藏品创建时间
     */
    @Field(name = "create_time", type = FieldType.Date, format = {},
            pattern = " yyyy-MM-dd'T'HH:mm:ss+08:00 || yyyy-MM-dd HH:mm:ss || strict_date_optional_time || epoch_millis")
    private Date createTime;

    /**
     * 藏品发售时间
     */
    @Field(name = "sale_time", type = FieldType.Date, format = {}, pattern = " yyyy-MM-dd'T'HH:mm:ss+08:00 || yyyy-MM-dd HH:mm:ss || strict_date_optional_time || epoch_millis")
    private Date saleTime;

    /**
     * 藏品上链时间
     */
    @Field(name = "sync_chain_time", type = FieldType.Date, format = {}, pattern = " yyyy-MM-dd'T'HH:mm:ss+08:00 || yyyy-MM-dd HH:mm:ss || strict_date_optional_time || epoch_millis")
    private Date syncChainTime;

    /**
     * 预约开始时间
     */
    @Field(name = "book_start_time", type = FieldType.Date, format = {}, pattern = " yyyy-MM-dd'T'HH:mm:ss+08:00 || yyyy-MM-dd HH:mm:ss || strict_date_optional_time || epoch_millis")
    private Date bookStartTime;

    /**
     * 预约结束时间
     */
    @Field(name = "book_end_time", type = FieldType.Date, format = {}, pattern = " yyyy-MM-dd'T'HH:mm:ss+08:00 || yyyy-MM-dd HH:mm:ss || strict_date_optional_time || epoch_millis")
    private Date bookEndTime;

    /**
     * 是否预约
     */
    @Field(name = "can_book", type = FieldType.Integer)
    private Integer canBook;

    /**
     * 藏品创建者id
     */
    private String creatorId;

    private Integer version;


    //封装了创建藏品的初始化参数
    public static Collection create(CollectionCreateRequest request){
        //将请求参数映射为藏品对象
        Collection collection = CollectionConvertor.INSTANCE.mapToEntity(request);
        //将已占库存设置为0，表示当前没有任何一个用户拥有该藏品
        collection.setOccupiedInventory(0L);
        //将藏品状态设置为初始化未处理状态
        collection.setState(CollectionStateEnum.INIT);
        //将可售库存设置为当前请求参数的藏品库存
        collection.setSaleableInventory(request.getQuantity());
        //设置版本号为1
        collection.setVersion(1);
        //返回初始化的藏品对象
        return collection;
    }

    public Collection remove(){
        this.state = CollectionStateEnum.REMOVED;
        return this;
    }
}
